//
//  XNPayInfoViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/14.
//

#import "XNPayInfoViewController.h"
#import "XNPayInfoBillInfoRequest.h"
#import "XNPayInfoApproveRequest.h"
#import "XNpayInfoBillInfoModel.h"

@interface XNPayInfoViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNpayInfoBillInfoModel *infoModel;

@end

@implementation XNPayInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNPayInfoBillInfoRequest *infoRequest = [[XNPayInfoBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNPayInfoBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        dispatch_group_leave(group);
    } failure:^(__kindof XNPayInfoBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_enter(group);
    XNPayInfoApproveRequest *approveRequest = [[XNPayInfoApproveRequest alloc] initWithOrderId:self.orderId];
    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNPayInfoApproveRequest * _Nonnull request) {
        weakSelf.approveArray = request.approveFlowArray;
        dispatch_group_leave(group);
    } failure:^(__kindof XNPayInfoApproveRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}



- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserName],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.createUserObject.departmentName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"申请信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请人: %@",self.infoModel.applicantName],
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.departmentName],
                               [NSString stringWithFormat:@"承运商： %@",self.infoModel.carrierName],
                               [NSString stringWithFormat:@"年份： %ld",self.infoModel.applyYear],
                               [NSString stringWithFormat:@"预算项： %@",self.infoModel.budgetItemName],
                               [NSString stringWithFormat:@"备注： %@",self.infoModel.remark],
                               nil];
    itemModel2.showItemArray = showItemArray2;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"运输账单明细信息";
    [self.infoModel.detailList enumerateObjectsUsingBlock:^(XNPayInfoDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"运输单号： %@",obj.ID],
                                   [NSString stringWithFormat:@"发车日期： %@",obj.departureTimeStr],
                                   [NSString stringWithFormat:@"车牌号： %@",obj.truckNo],
                                   [NSString stringWithFormat:@"出发地： %@",obj.startPlaceName],
                                   [NSString stringWithFormat:@"目的地： %@",obj.endPlaceName],
                                   [NSString stringWithFormat:@"运费： %.3f",obj.transportMoney],
                                   [NSString stringWithFormat:@"押车费： %.3f",obj.escortMoney],
                                   [NSString stringWithFormat:@"代垫费： %.3f",obj.prepaidMoney],
                                   [NSString stringWithFormat:@"其他费用： %.3f",obj.otherMoney],
                                   [NSString stringWithFormat:@"费用合计： %.3f",obj.allMoney],
                                   [NSString stringWithFormat:@"是否含税： %@",obj.isDutyStr],
                                   [NSString stringWithFormat:@"需求车型： %@",obj.needTruckTypeName],
                                   [NSString stringWithFormat:@"实际车型： %@",obj.realTruckTypeName],
                                   [NSString stringWithFormat:@"司机： %@",obj.truckDriver],
                                   [NSString stringWithFormat:@"客户： %@",obj.customerName],
                                   [NSString stringWithFormat:@"货物类别： %@",obj.productInfo],
                                   [NSString stringWithFormat:@"备注： %@",obj.remark],
                                   nil];
        temp.showItemArray = showItemArray3;
        [showModel3.showDataArray addObject:temp];
    }];
    [self.showArray addObject:showModel3];
    
    XNTableShowDataModel *showModel7 = [[XNTableShowDataModel alloc] init];
    showModel7.showType = XNShowType_Normal;
    showModel7.title = @"预算信息";
    XNShowItemModel *itemModel7 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray7 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"%@预算总金额:%.3f",self.infoModel.budgetItemObject.budgetInfo,self.infoModel.budgetItemObject.allMoney],
                               [NSString stringWithFormat:@"已报销： %.3f",self.infoModel.budgetItemObject.reimbursedMoney],
                               [NSString stringWithFormat:@"报销中： %.3f",self.infoModel.budgetItemObject.reimbursingMoney],
                               [NSString stringWithFormat:@"剩余可报销： %.3f",self.infoModel.budgetItemObject.remainReimburseMoney],
                               [NSString stringWithFormat:@"已申请未报销： %.3f",self.infoModel.budgetItemObject.applyedMoney],
                               [NSString stringWithFormat:@"在申请未报销： %.3f",self.infoModel.budgetItemObject.applyingMoney],
                               [NSString stringWithFormat:@"剩余可申请： %.3f",self.infoModel.budgetItemObject.remainApplyMoney],
                               nil];
    itemModel7.showItemArray = showItemArray7;
    [showModel7.showDataArray addObject:itemModel7];
    [self.showArray addObject:showModel7];
    
    
    XNTableShowDataModel *approveModel = [[XNTableShowDataModel alloc] init];
    approveModel.showType = XNShowType_Approve;
    approveModel.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModel.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModel];
    
}

@end
